/*
 * np_dbg.h
 *
 *  Created on: Mar 18, 2011
 *      Author: jeff_zheng
 *   Copyright: NetPerform Technology
 */

#ifndef NP_DBG_H_
#define NP_DBG_H_

#ifdef __cplusplus
extern "C" {
#endif
#include "np_common.h"
#include <inttypes.h>
#define np_hex_prn(fmt, ...) \
	do {\
	plen = snprintf(buf+off, left, fmt, ## __VA_ARGS__); \
	off += plen; \
	if(plen>=left) \
	{ \
		buf[buf_size-1] = 0; \
		return; \
	} \
	left -= plen; \
	}while(0);

static inline void np_hex_dump_str(void* to_dump, uint32_t size, char* buf, uint32_t buf_size)
{
	uint8_t* src = (uint8_t*)to_dump;
	uintptr_t loop;
	uintptr_t base = (uintptr_t)src;
	uintptr_t base_rnd = base & 0xFFFFFFFFFFFFFFF0;
	uint32_t off = 0;
	uint32_t left = buf_size-1;
	uintptr_t plen;

	np_hex_prn("0x%lx:", base_rnd);
	for(loop = base_rnd; loop<base;loop++)
	{
		np_hex_prn("   ");
	}
	uintptr_t loop_max = 16 - (base - base_rnd);
	for(loop = 0; loop<loop_max; loop++)
	{
		np_hex_prn(" %02x", src[loop]);
	}
	np_hex_prn("\n");
	for(; (loop+16) < size; loop++)
	{
		base_rnd += 16;
		np_hex_prn("0x%lx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x |"
				"%c%c%c%c%c%c%c%c | %c%c%c%c%c%c%c%c\n",
				base_rnd,
				src[loop], src[loop+1], src[loop+2], src[loop+3],
				src[loop+4], src[loop+5], src[loop+7], src[loop+6],
				src[loop+8], src[loop+9], src[loop+10], src[loop+11],
				src[loop+12], src[loop+13], src[loop+14], src[loop+15],
				src[loop], src[loop+1], src[loop+2], src[loop+3],
				src[loop+4], src[loop+5], src[loop+7], src[loop+6],
				src[loop+8], src[loop+9], src[loop+10], src[loop+11],
				src[loop+12], src[loop+13], src[loop+14], src[loop+15]);
	}
	base_rnd += 16;
	np_hex_prn("0x%lx:", base_rnd);
	for(; loop<size; loop++)
	{
		np_hex_prn(" %02x", src[loop]);
	}
	np_hex_prn("\n");

}


#ifdef __cplusplus
}
#endif

#endif /* NP_DBG_H_ */
